/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.gra.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.gra.dao.PaperDao;
import com.thinkgem.jeesite.modules.gra.dao.QuestionDao;
import com.thinkgem.jeesite.modules.gra.entity.Paper;
import com.thinkgem.jeesite.modules.gra.entity.Question;

/**
 * 试卷管理Service
 * @author wuzengli
 * @version 2017-05-04
 */
@Service
@Transactional(readOnly = true)
public class PaperService extends CrudService<PaperDao, Paper> {

	@Autowired
	private QuestionDao questionDao;
	
	public Paper get(String id) {
		Paper paper = super.get(id);
		paper.setQuestionList(questionDao.findList(new Question(paper)));
//		paper.setScoreList(scoreDao.findList(new Score(paper)));
		return paper;
	}
	
	public List<Paper> findList(Paper paper) {
		return super.findList(paper);
	}
	
	public Page<Paper> findPage(Page<Paper> page, Paper paper) {
		return super.findPage(page, paper);
	}
	
	@Transactional(readOnly = false)
	public void save(Paper paper) {
		super.save(paper);
		for (Question question : paper.getQuestionList()){
			if (question.getId() == null){
				continue;
			}
			if (Question.DEL_FLAG_NORMAL.equals(question.getDelFlag())){
				if (StringUtils.isBlank(question.getId())){
					question.setPaper(paper);
					question.preInsert();
					questionDao.insert(question);
				}else{
					question.preUpdate();
					questionDao.update(question);
				}
			}else{
				questionDao.delete(question);
			}
		}
		/*for (Score score : paper.getScoreList()){
			if (score.getId() == null){
				continue;
			}
			if (Score.DEL_FLAG_NORMAL.equals(score.getDelFlag())){
				if (StringUtils.isBlank(score.getId())){
					score.setPaper(paper);
					score.preInsert();
					scoreDao.insert(score);
				}else{
					score.preUpdate();
					scoreDao.update(score);
				}
			}else{
				scoreDao.delete(score);
			}
		}*/
	}
	
	@Transactional(readOnly = false)
	public void delete(Paper paper) {
		super.delete(paper);
		questionDao.delete(new Question(paper));
	}
	
}